<?php

namespace app\superadmin\controller;

use app\common\controller\SuperadminBase;
use app\superadmin\model\Admin as Model;
use app\superadmin\model\Role as RoleModel;
use think\Controller;

use think\Db;

use think\Request;

use think\Session;



class Cadmin extends SuperadminBase

{



    public function index()

    {

        return $this->fetch();

    }



    //获取商品列表-无限定条件-查询出所有

    public function gList(Request $request)

    {

        //获取每页显示的条数

        $limit = $request->param('limit');

        //获取当前页数

        $page = $request->param('page');


        $name = $request->param('name');


        $goodList = Model::with('roleinfo')->where('type','<',3)->order('create_time desc')->paginate($limit, false, ['page' => $page])->toArray();

        return json(['code' => 0, 'msg' => '', 'count' => $goodList['total'], 'data' => $goodList['data']]);

    }

    
     public function add()

    {   

        $rolelist = RoleModel::order('id','desc')->select();
       
        $this->assign('rolelist', $rolelist);
        return $this->fetch('cadmin/add');

    }


    public function getTree($items,$pid ="pid") {
        $map  = [];
        $tree = [];
        foreach ($items as &$it){ $map[$it['id']] = &$it; }  //数据的ID名生成新的引用索引树
        foreach ($items as &$at){
            $parent = &$map[$at[$pid]];
            if($parent) {
                $parent['children'][] = &$at;
            }else{
                $tree[] = &$at;
            }
        }
        return $tree;
    }



    public function addgood()

    {

        $data     = input('post.');

        $data['phone'] = $data['account'];

        $data['password'] = md5($data['password']);
        
        Model::create($data);

        
        return 1;
    }





    public function edit()

    {

        $id       = input('id');

        $info = Model::where('id',$id)->find();

        $rolelist = RoleModel::order('id','desc')->select();

        $this->assign('info', $info);
        $this->assign('rolelist', $rolelist);
        return $this->fetch('cadmin/edit');

    }



    public function edit1()

    {
        $data     = input('post.');

        $data['phone'] = $data['account'];

        if (!empty($data['password'])) {
            $data['password'] = md5($data['password']);
        }
        
        Model::where('id',$data['id'])->update($data);

        return 1;

    }




    // 删除

    public function delGood()

    {

        $id = $this->request->param('id');

        Model::destroy($id);
        return 1;

    }







    public function editSxj()

    {
        $id    = $this->request->param('id');

        $is_xj = $this->request->param('is_xj');

        $info = Model::where('id',$id)->find();

        $rows  = Model::where('id', $id)->update(['is_disable' => $is_xj]);


        if ($rows) {

            return 1;

        } else {

            return 2;

        }

    }




}

